package org.mm4juser.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Date;

public class JWTTokenUtil {
	public static final String SECRET_KEY = "HK4aDkYxmdYG4ZbwrjnCKJNGNTtAGR7H7xC4fTXzy4GWHSTxcj3FnbkMmNQSTMbSrnJm5dSeWXxeQvRfKe7jty8sG7fHzBrmTDp5fjYMjTNRzM7KN7Xc5uHSVDUWDfEhhFFZZp8UVp654yfbdBr6KzdS5p8Ut8yYCfFErAMKxVUt7VxKG2QsXYkRFBZ2hxTFUMTa87ftcundbbCGta2C4AcZCQfAtBat7YJDfpWw3szjwef4ENadMAQbWkszNA82";
	
	public static final SecretKey KEY = Keys.hmacShaKeyFor(SECRET_KEY.getBytes(StandardCharsets.UTF_8));
	// token保质期为30天
	private static final long EXPIRATION_TIME = 7 * 1000 * 60 * 60 * 24;
	
	//例
	//String jwt = Jwts.builder()                     // (1)
	//
	//    .header()                                   // (2) 设置头
	//        .keyId("aKeyId")
	//        .and()
	//
	//    .subject("Bob")                             // (3) JSON Claims, or
	//    .content(aByteArray, "text/plain")             any byte[] content, with media type
	//
	//    .signWith(signingKey)                       // (4) if signing, or
	//    .encryptWith(key, keyAlg, encryptionAlg)       if encrypting
	//
	//    .compact();                                  (5)
	//例
	//String jws = Jwts.builder()
	//
	//    .issuer("me")
	//    .subject("Bob")
	//    .audience().add("you").and()
	//    .expiration(expiration) //a java.util.Date
	//    .notBefore(notBefore) //a java.util.Date
	//    .issuedAt(new Date()) // for example, now
	//    .id(UUID.randomUUID().toString()) //just an example id
	//
	//    /// ... etc ...
	
	/**
	 * 登陆时签发token
	 *
	 * @param email 邮箱
	 */
	public static String getToken(
		String email) {
		
		return Jwts.builder()
			.subject(email)
			.signWith(KEY)
			.compact();
	}
}
